package com.xcity.game.battle.pve;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.battle.AbstractBattler;
import com.xcity.game.battle.BattleContext;
import com.xcity.game.battle.BattleException;
import com.xcity.game.battle.pve.employee.EmployeeSkillBattleContext;
import com.xcity.game.battle.pve.employee.EmployeeSkillBattleContextFactory;
import com.xcity.game.employee.skill.EmployeeSkill;
import com.xcity.game.employee.skill.SkillGroups2;

import gnu.trove.map.TByteObjectMap;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TByteObjectHashMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TLongObjectHashMap;

@Deprecated
public class PlayerBattler extends AbstractBattler {

	protected Player player;
	protected List<Employee> employees; // 携带的员工
	protected TLongObjectMap<TIntObjectMap<EmployeeSkillBattleContext>> employeeSkillsContext;
	protected TByteObjectMap<List<EmployeeSkillBattleContext>> group2skills;
	
	protected static final Logger LOG = PVEService.LOG;
	
	public PlayerBattler(Player player, List<Employee> employees) {
		super(PVEUtils.getPlayerInitHP(player, employees), PVEUtils.calcAttack(player, employees));
		this.player = player;
		this.employees = employees;
		this.employeeSkillsContext = new TLongObjectHashMap<>();
		this.group2skills = new TByteObjectHashMap<>();
	}
	
	@Override
	public void init(BattleContext context) {
		this.context = context;
		for (int i = 0; i < employees.size(); i++) {
			Employee e = employees.get(i);
			TIntObjectMap<EmployeeSkillBattleContext> contexts = new TIntObjectHashMap<>();
			employeeSkillsContext.put(e.getId(), contexts);
			List<EmployeeSkill> skills = null;//e.getSkills();
			for (int j = 0, size = skills != null ? skills.size() : 0; j < size; j++) {
				EmployeeSkill skill = skills.get(j);
				EmployeeSkillBattleContext ctx = EmployeeSkillBattleContextFactory.create(this, skill);
				if (ctx != null) {
					contexts.put(skill.getId(), ctx);
					List<EmployeeSkillBattleContext> list = group2skills.get(skill.getTemplate().getGroup());
					if (list == null) {
						list = new ArrayList<>();
						group2skills.put(skill.getTemplate().getGroup(), list);
					}
					list.add(ctx);
				}
			}
		}
	}
	
	public EmployeeSkillBattleContext getSkillContext(long eid, int skillId) {
		TIntObjectMap<EmployeeSkillBattleContext> map = employeeSkillsContext.get(eid);
		return map != null ? map.get(skillId) : null;
	}
	
	public List<EmployeeSkillBattleContext> getSkillContextsByGroup(SkillGroups2 group) {
		return group2skills.get((byte) group.ordinal());
	}
	
	public boolean batchTrigger(SkillGroups2 group) throws BattleException {
		boolean triggered = false;
		List<EmployeeSkillBattleContext> ctxs = getSkillContextsByGroup(group);
		for (int i = 0, size = ctxs != null ? ctxs.size() : 0; i < size; i++) {
			EmployeeSkillBattleContext ctx = ctxs.get(i);
			if (ctx.trigger()) {
				((PVEBattle) getContext()).triggered(ctx.getSkill());
				triggered = true;
			}
		}
		return triggered;
	}
	
	public float batchCalcFloat(SkillGroups2 group) {
		float val = 0f;
		List<EmployeeSkillBattleContext> ctxs = getSkillContextsByGroup(group);
		for (int i = 0, size = ctxs != null ? ctxs.size() : 0; i < size; i++) {
			EmployeeSkillBattleContext ctx = ctxs.get(i);
			try {
				Float v = ctx.calc(ctx.getSkill().getId());
				val += v;
			} catch (Exception e) {
				LOG.error("[PVE ESKILL CALC ERROR]ID[" + player.getId() + "]SKILL[" + ctx.getSkill().getId() + "]", e);
			}
		}
		return val;
	}

}
